package com.itboye.eweilisten.util.widget;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.model.SouSuoBean;
import com.itboye.eweilisten.util.sputil.SuoSouUtil;

import java.util.ArrayList;
import java.util.List;

import static com.itboye.eweilisten.app.MyApplication.context;

/**
 * Created by itboye on 2017/3/11.
 */

public class FlowLayoutClassUtil {
    static FlowLayoutFragment flowLayoutOne;
    static FlowLayoutFragment flowLayoutTwo;
    static FlowLayoutFragment flowLayoutThree;
    static TextView tvWancheng;
    static String id;


    public static void onFna(List<SouSuoBean> been, PopupWindows windows, List<String> strings, Context context, View v) {
        strings.clear();

        View view = View.inflate(context, R.layout.add_popup_dialog, null);
        windows = new PopupWindows(context, v, view);
        flowLayoutOne = (FlowLayoutFragment) view.findViewById(R.id.flowlayoutOne);
        flowLayoutTwo = (FlowLayoutFragment) view.findViewById(R.id.flowlayoutTwo);
        flowLayoutThree = (FlowLayoutFragment) view.findViewById(R.id.flowlayoutThree);
        tvWancheng = (TextView) view.findViewById(R.id.tvWancheng);
        OnWanCheng(tvWancheng,windows);
        TextView tvReset = (TextView) view.findViewById(R.id.tvReset);


        Log.d("SuoSouUtil", SuoSouUtil.onTextView(been, strings) + "");
        Log.d("SuoSouUtilone", SuoSouUtil.onTextViewTwo(been, strings) + "");
        Log.d("SuoSouUtiltwo", SuoSouUtil.onTextViewThree(been, strings) + "");
        // 添加数据
        flowLayoutOne.addTags(context, SuoSouUtil.onTextView(been, strings));

        // 设置是否单选
        flowLayoutOne.setRadio(false);
        onFlowFragment(flowLayoutOne, been, strings);
        onFlowFragmentTwo(flowLayoutTwo, been, strings);
        onFlowFragmentThree(flowLayoutThree, been);
        onTextView(tvReset, been, strings);
    }

    public static void OnWanCheng(TextView tvWancheng, final PopupWindows windows) {
        tvWancheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Chiid chiid=(Chiid) context;
                chiid.onChiId(id);
                windows.dismiss();
            }
        });
    }

    public static void onTextView(View v, final List<SouSuoBean> been, final List<String> strings) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flowLayoutOne.addTags(MyApplication.instance, SuoSouUtil.onTextViewTwo(been, strings));
                flowLayoutTwo.addTags(MyApplication.instance, SuoSouUtil.onTextViewTwo(been, strings));
                flowLayoutThree.addTags(MyApplication.instance, SuoSouUtil.onTextViewTwo(been, strings));
            }
        });
    }

    public static void onFlowFragment(FlowLayoutFragment fragment, final List<SouSuoBean> been, final List<String> strings) {
        fragment.setTagOnClickListener(new FlowLayoutFragment.TagOnClickListener() {

            @Override
            public void tagOnClickListener(int index, String str,
                                           boolean isChecked, int num) {
                // TODO Auto-generated method stub
                Log.e("tagOnClickListenernum", "index = " + index + "   str = "
                        + str + "  isChecked = " + isChecked + "strings" + num);
                if (index != 0) {
                    strings.clear();
                    // 添加数据
                    flowLayoutThree.addTags(context, strings);
                } else if (isChecked == false || (index != 0)) {
                    strings.clear();
                    // 添加数据
                    flowLayoutTwo.addTags(MyApplication.instance, strings);
                    // 添加数据
                    flowLayoutThree.addTags(context, strings);
                } else {
                    // 添加数据
                    flowLayoutTwo.addTags(MyApplication.instance, SuoSouUtil.onTextViewTwo(been, strings));
                }


            }
        });
    }

    static List<String> ids = new ArrayList<String>();
    static List<String> stringList = new ArrayList<>();
    static String name;
    static int indexuntil;


    public static void onFlowFragmentTwo(FlowLayoutFragment fragment, final List<SouSuoBean> been, final List<String> strings) {

        fragment.setTagOnClickListener(new FlowLayoutFragment.TagOnClickListener() {

            @Override
            public void tagOnClickListener(int index, String str,
                                           boolean isChecked, int num) {
                // TODO Auto-generated method stub

                for (int i = 0; i < been.size(); i++) {
                    Log.e("tagOnClickListenertwo", "index = " + index + "   str = "
                            + str + "  isChecked = " + isChecked + "strings" + been.get(i).getUnits().get(index).getUnit_id());
                }
                if (index != 0) {
                    strings.clear();
                    // 添加数据
                    flowLayoutThree.addTags(context, strings);
                } else if (isChecked == false || (index != 0)) {
                    strings.clear();
                    // 添加数据
                    // 添加数据
                    flowLayoutThree.addTags(context, strings);
                    Log.d("nunumnum3", num + "");
                } else {
                    // 添加数据
                    Log.d("nunumnum4", num + "");
                    indexuntil = index;
                    flowLayoutThree.addTags(MyApplication.instance, SuoSouUtil.onTextViewThree(been, strings));
                }

            }
        });
    }

    public static void onFlowFragmentThree(FlowLayoutFragment fragment, final List<SouSuoBean> been) {
        fragment.setTagOnClickListener(new FlowLayoutFragment.TagOnClickListener() {
            @Override
            public void tagOnClickListener(int index, String str,
                                           boolean isChecked, int num) {
                // TODO Auto-generated method stub
                Log.e("tagOnClickListener", "index = " + index + "   str = "
                        + str + "  isChecked = " + isChecked);
                for (int i = 0; i < been.size(); i++) {

                    id=been.get(i).getUnits().get(indexuntil).getChild().get(index).getUnit_id();
                    Log.d("ididididid",id);
                    Log.e("tagOnClickListenertwo", indexuntil + "index = " + index + "   str = "
                            + str + "  isChecked = " + isChecked + "strings" + been.get(i).getUnits().get(indexuntil).getChild().get(index).getUnit_id());
                }
            }
        });
    }

    public interface Chiid{
        void onChiId(String id);
    }
}
